package com.cz.run.beans.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CAIYJ
 * @Description: TODO()
 * @Date:2018/5/15 14:07
 */
@ApiModel
public class PageDTO<T> implements Serializable {

    /**
     * 页码
     */
    @ApiModelProperty(value = "页码", example = "1")
    private int currentPage = 1;

    /**
     * 每页数
     */
    @ApiModelProperty(value = "每页数", example = "10")
    private int pageSize = 10;

    /**
     * 起始数
     */
    private int pageIndex = 0;

    /**
     * 总页数
     */
    private int totalPage = 0;

    /**
     * 总记录数
     */
    @ApiModelProperty(value = "总记录数", example = "入参是不用管，返回集合的总记录数")
    private int totalRecord = 0;

    @ApiModelProperty(value = "实体", example = "入参是不用管，返回集合")
    private List<T> content = new ArrayList<T>();

    /**
     * 是否有上一页
     */
    private boolean hasPrePage = false;

    /**
     * 是否有下一页
     */
    private boolean hasNextPage = false;

    public PageDTO() {

    }

    public PageDTO(int totalRecord, List<T> content) {
        super();
        this.totalRecord = totalRecord;
        this.content = content;
    }

    public PageDTO(int pageSize, int currentPage, int totalRecord, List<T> content) {
        this.pageSize = pageSize;
        this.currentPage = (currentPage == 0 ? 1 : currentPage);
        this.totalRecord = totalRecord;
        this.content = content;
        this.pageIndex = PageDTO.calPageIndex(currentPage, pageSize);
        this.totalPage = PageDTO.calTotalPage(totalRecord, pageSize);
        this.hasPrePage = PageDTO.calHasPrePage(currentPage);
        this.hasNextPage = PageDTO.calHasNextPage(currentPage, totalPage);
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public boolean isHasPrePage() {
        return hasPrePage;
    }

    public void setHasPrePage(boolean hasPrePage) {
        this.hasPrePage = hasPrePage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public static Integer calPageIndex(Integer currentPage, Integer pageSize) {
        return (currentPage - 1) * pageSize;
    }

    public static Integer calTotalPage(Integer totalRecords, Integer pageSize) {
        if (totalRecords % pageSize == 0) {
            return totalRecords / pageSize;
        } else {
            return totalRecords / pageSize + 1;
        }
    }

    public static boolean calHasPrePage(Integer currentPage) {
        return currentPage == 1 ? false : true;
    }

    public static boolean calHasNextPage(Integer currentPage, Integer totalPage) {
        if (currentPage.equals(totalPage) || totalPage == 0) {
            return false;
        } else {
            return true;
        }
    }
}
